package com.fantasy.service.impl;

import com.fantasy.dao.UserDao;
import com.fantasy.dao.impl.UserDaoImpl;
import com.fantasy.entity.Admin;
import com.fantasy.entity.Page;
import com.fantasy.entity.User;
import com.fantasy.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = null;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public int insert(User user) {
        return userDao.insert(user);
    }

    @Override
    public int delete(int id) {
        return userDao.delete(id);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public User queryById(int id) {
        return userDao.queryById(id);
    }

    @Override
    public User queryByPhoneAndPassword(String phone, String password) {
        return userDao.queryByPhoneAndPassword(phone,password);
    }

    @Override
    public User queryByPhone(String phone) {
        return userDao.queryByPhone(phone);
    }

    @Override
    public List<User> queryAll() {
        return userDao.queryAll();
    }

    @Override
    public Page<User> pageQuery(int pageNo, int pageSize) {

        Page<User> page=new Page();

        //设置开始页码
        page.setPageNo(pageNo);

        //设置当前页显示的数量
        page.setPageSize(pageSize);

        //设置当前页数据
        page.setItems(userDao.queryForPageItems(pageNo,pageSize));

        //设置数据总数
        Integer totalCount = userDao.queryForPageTotalCount();
        page.setPageTotalCount(totalCount);

        //设置总页码
        int count = totalCount / pageSize;
        if (totalCount % pageSize!=0){
            count+=1;
        }
        page.setPageTotal(count);

        return page;
    }
}
